﻿// MotorDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "MotorDlg.h"
#include "afxdialogex.h"


// CMotorDlg 对话框

IMPLEMENT_DYNAMIC(CMotorDlg, CDialogEx)

CMotorDlg::CMotorDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MOTOR, pParent)
{

}

CMotorDlg::~CMotorDlg()
{
}

void CMotorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_MOTORID, m_comboAxisNum);
	DDX_Control(pDX, IDC_EDIT_POSITION, m_editCrtPos);
	DDX_Control(pDX, IDC_EDIT_MOVESPEED, m_editSpeedMove);
	DDX_Control(pDX, IDC_EDIT_HOME_SPEED, m_editSpeedHome);
	DDX_Control(pDX, IDC_EDIT_ACCSPEED, m_editSpeedAcc);
	DDX_Control(pDX, IDC_EDIT_STEP_DIS, m_editReDis);
	DDX_Control(pDX, IDC_COMBO_DIRECTION, m_comboDirection);
	DDX_Control(pDX, IDC_CHECK_SERVOON, m_btnServoOn);
	DDX_Control(pDX, IDC_CHECK_HOME, m_btnHome);
	DDX_Control(pDX, IDC_CHECK_LIMITP, m_btnLmtP);
	DDX_Control(pDX, IDC_CHECK_LIMITM, m_btnLmtN);
	DDX_Control(pDX, IDC_CHECK_ALARM, m_btnAlarm);
	DDX_Control(pDX, IDC_BUTTON_SERVO_ON, m_bBtnServoOn);
	DDX_Control(pDX, IDC_EDIT_STARTSPEED, m_editStartSpeed);
}


BEGIN_MESSAGE_MAP(CMotorDlg, CDialogEx)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CMotorDlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_SERVO_ON, &CMotorDlg::OnBnClickedButtonServoOn)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CMotorDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_RESET_ALARM, &CMotorDlg::OnBnClickedButtonResetAlarm)
	ON_BN_CLICKED(IDC_BUTTON_MOVE, &CMotorDlg::OnBnClickedButtonMove)
	ON_BN_CLICKED(IDC_BUTTON_HOME, &CMotorDlg::OnBnClickedButtonHome)
	ON_BN_CLICKED(IDC_BUTTON_STEP_P, &CMotorDlg::OnBnClickedButtonStepP)
	ON_BN_CLICKED(IDC_BUTTON_STEP_M, &CMotorDlg::OnBnClickedButtonStepM)
	ON_CBN_SELCHANGE(IDC_COMBO_MOTORID, &CMotorDlg::OnCbnSelchangeComboMotor)
END_MESSAGE_MAP()


// CMotorDlg 消息处理程序

BOOL CMotorDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetTimer(TIMER_ID_AXIS_STATUS, 500, NULL);

	gSystemMgr.m_ptrMotorDlg = this;

	m_mapAxisSetting = *gConfigMgr.GetAxisSettingDict();

	CreateAxisList();
	ListAllAxis();
	ShowCrtAxisMsg();


	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CMotorDlg::OnCancel()
{
	KillTimer(TIMER_ID_AXIS_STATUS);

	CDialogEx::OnCancel();
}


void CMotorDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == TIMER_ID_AXIS_STATUS)
	{
		GetCrtAxisStatus();
	}
	CDialogEx::OnTimer(nIDEvent);
}

void CMotorDlg::GetCrtAxisStatus()
{

	m_btnServoOn.SetCheck(!gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].SevON);
	m_btnAlarm.SetCheck(gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].ALM);
	m_btnLmtP.SetCheck(gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].EL_Z);
	m_btnLmtN.SetCheck(gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].EL_F);
	m_btnHome.SetCheck(gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].ORG);


	double nDis = gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_nCurPositon;
	CString cstr;
	cstr.Format(L"%.1f", nDis);
	m_editCrtPos.SetWindowTextW(cstr);

	//double NewSpeed = 0;
	//iret = dmc_read_current_speed_unit(m_nCard, m_nAxis, &NewSpeed);          //获取当前轴速度
	//string.Format("当前速度：%.2f", NewSpeed);
	//GetDlgItem(IDC_STATIC_NowSpe)->SetWindowText(string);

	//DWORD status = dmc_check_done(m_nCard, m_nAxis);           //判断当前轴状态
	//if (status == 1)
	//{
	//	GetDlgItem(IDC_STATIC_STATUS)->SetWindowText("当前状态：静止");
	//}
	//else
	//{
	//	GetDlgItem(IDC_STATIC_STATUS)->SetWindowText("当前状态：运动");
	//}
}

void CMotorDlg::CreateAxisList()
{

}

void CMotorDlg::ListAllAxis()
{
	m_comboDirection.ResetContent();
	m_comboDirection.AddString(L"反向");
	m_comboDirection.AddString(L"正向");
	m_comboDirection.SetCurSel(1);

	int nCrtDir = m_comboDirection.GetCurSel();

	m_comboAxisNum.ResetContent();
	for (auto it = m_mapAxisSetting.begin(); it != m_mapAxisSetting.end(); it++)
	{
		CString cstr;
		//cstr.Format(L"%d", it->first);
		switch (it->first) {
		case 0:
			cstr.Format(L"%d:X", it->first);
			break;
		case 1:
			cstr.Format(L"%d:Y", it->first);
			break;
		case 2:
			cstr.Format(L"%d:Z", it->first);
			break;
		default:
			cstr.Format(L"%d", it->first);
			break;
		}
		m_comboAxisNum.AddString(cstr);

		//这部分是List
		//m_listAxis.DeleteItem(0);
		//cstr.Format(L"%d", it->second.m_nAxisId);
		//m_listAxis.InsertItem(0, cstr);

		//cstr = ATL::CW2A(it->second.m_wstrAxisName.c_str());
		//m_listAxis.SetItemText(0, 1, cstr);

		//cstr = ATL::CW2A(it->second.m_wstrProportion.c_str());
		//m_listAxis.SetItemText(0, 2, cstr);
	}
	m_comboAxisNum.SetCurSel(0);

	m_crtAxisSetting = m_mapAxisSetting[0];
}

void CMotorDlg::ShowCrtAxisMsg()
{
	CString cstr;
	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedHome);
	m_editSpeedHome.SetWindowTextW(cstr);

	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedMove);
	m_editSpeedMove.SetWindowTextW(cstr);

	cstr.Format(L"%.2f", m_crtAxisSetting.m_nSpeedAcc);
	m_editSpeedAcc.SetWindowTextW(cstr);

	cstr.Format(L"%d", m_crtAxisSetting.m_nStartSpeed);
	m_editStartSpeed.SetWindowTextW(cstr);

	m_editCrtPos.SetWindowTextW(L"0");
	m_editReDis.SetWindowTextW(L"0");
}


void CMotorDlg::OnBnClickedButtonSave()
{
	CString cstrId;
	int nCrtIndex = m_comboAxisNum.GetCurSel();
	CString cstr;
	m_editSpeedHome.GetWindowTextW(cstr);
	m_crtAxisSetting.m_nSpeedHome = _ttoi(cstr);
	m_editSpeedMove.GetWindowTextW(cstr);
	m_crtAxisSetting.m_nSpeedMove = _ttoi(cstr);
	m_editSpeedAcc.GetWindowTextW(cstr);
	m_crtAxisSetting.m_nSpeedAcc = _ttof(cstr);
	m_editStartSpeed.GetWindowTextW(cstr);
	m_crtAxisSetting.m_nStartSpeed = _ttoi(cstr);
	cstrId.Format(L"%d", nCrtIndex);
	gConfigMgr.UpdateAxisSetting(cstrId.GetBuffer(), m_crtAxisSetting);

	m_mapAxisSetting = *gConfigMgr.GetAxisSettingDict();
	gSystemMgr.MotorReadConfig();
	AfxMessageBox(L"参数保存成功");
}


void CMotorDlg::OnBnClickedButtonServoOn()
{
	if (!gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bServoOn)
	{
		m_bBtnServoOn.SetWindowTextW(L"Servo_On");
	}
	else
	{
		m_bBtnServoOn.SetWindowTextW(L"Servo_Off");
	}

	if (!gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].SevON)
	{
		gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].ServoOff();

	}
	else
	{
		gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].ServoOn();
	}
}


void CMotorDlg::OnBnClickedButtonStop()
{
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].StopMove();
}


void CMotorDlg::OnBnClickedButtonResetAlarm()
{
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].ResetAlarm();
}


void CMotorDlg::OnBnClickedButtonMove()
{
	int nCrtDir = m_comboDirection.GetCurSel();
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].MotorMoveContinue(nCrtDir);
}


void CMotorDlg::OnBnClickedButtonHome()
{
	int nCrtDir = m_comboDirection.GetCurSel();
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].HomeSearch(nCrtDir);
}


void CMotorDlg::OnBnClickedButtonStepP()
{
	CString cstr;
	m_editReDis.GetWindowTextW(cstr);
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_sAxisSetting.m_dTargetPos = _ttof(cstr);
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].MotorMoveStep();
}


void CMotorDlg::OnBnClickedButtonStepM()
{
	CString cstr;
	m_editReDis.GetWindowTextW(cstr);
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_sAxisSetting.m_dTargetPos = -_ttof(cstr);
	gSystemMgr.m_vCMotor[m_crtAxisSetting.m_nAxisId].MotorMoveStep();
}

void CMotorDlg::OnCbnSelchangeComboMotor()
{
	int nCrtIndex = m_comboAxisNum.GetCurSel();
	m_crtAxisSetting = m_mapAxisSetting[nCrtIndex];

	CString cstr;
	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedHome);
	m_editSpeedHome.SetWindowTextW(cstr);
	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedMove);
	m_editSpeedMove.SetWindowTextW(cstr);
	cstr.Format(L"%.2f", m_crtAxisSetting.m_nSpeedAcc);
	m_editSpeedAcc.SetWindowTextW(cstr);
	cstr.Format(L"%d", m_crtAxisSetting.m_nStartSpeed);
	m_editStartSpeed.SetWindowTextW(cstr);

	ShowCrtAxisMsg();
}

